package com.tfswx.jkgl.designer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.tfswx.jkgl.core.bean.Jkdy;
import com.tfswx.jkgl.core.bean.JkglRuntimeData;
import com.tfswx.jkgl.core.bean.Mxgxdy;
import com.tfswx.jkgl.core.bean.SjmxXtMxdy;
import com.tfswx.jkgl.core.bean.TSjmxXtMxstgx;
import com.tfswx.jkgl.core.bean.Zddy;
import com.tfswx.jkgl.core.utils.sm4.EncryptSm4InputStream;
import com.tfswx.jkgl.designer.dao.ExportMapper;
import com.tfswx.jkgl.designer.dto.export.ConfigExportInputDTO;
import com.tfswx.jkgl.designer.service.ExportService;
import com.tfswx.jkgl.designer.service.FsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author zhanglj
 * @description: 导出指定项目接口定义数据项
 * @date 2021/12/13 20:19
 **/
@Slf4j
@Service
public class ExportServiceImpl implements ExportService {

    @Resource
    private ExportMapper exportMapper;
    @Resource
    private FsService fsService;

    /**
     * 导出指定项目数据
     *
     * @param request
     */
    @Override
    public void exportConfigFile(final ConfigExportInputDTO request) {
        JkglRuntimeData jkglRuntimeData = getJkglRuntimeData(request.getXmbm());
        byte[] bytes = JSONObject.toJSONString(jkglRuntimeData).getBytes(StandardCharsets.UTF_8);
        try (EncryptSm4InputStream encryptSm4InputStream = new EncryptSm4InputStream(bytes)) {
            fsService.download(encryptSm4InputStream, "jkconf-" + DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        } catch (IOException e) {
            log.error("资源关闭失败", e);
        }
    }

    @Override
    public JkglRuntimeData getJkglRuntimeData(String xmbm) {
        JkglRuntimeData jkglRuntimeData = new JkglRuntimeData();

        List<SjmxXtMxdy> tSjmxXtMxdies = this.exportMapper.queryAllMxdyByXmbm(xmbm);
        List<TSjmxXtMxstgx> sjmxXtMxstgxes = this.exportMapper.queryAllMxstgxByXmbm(xmbm);

        jkglRuntimeData.setMxdyList(new ConcurrentHashMap<>());
        if (Objects.nonNull(tSjmxXtMxdies) && !tSjmxXtMxdies.isEmpty()) {
            Map<String, List<SjmxXtMxdy>> collect = tSjmxXtMxdies.stream().collect(Collectors.groupingBy(SjmxXtMxdy::getMXBM, Collectors.toList()));
            Iterator<Map.Entry<String, List<SjmxXtMxdy>>> iterator = collect.entrySet().iterator();
            Map<String, SjmxXtMxdy> mxdyMap = new HashMap<>(16);
            while (iterator.hasNext()) {
                Map.Entry<String, List<SjmxXtMxdy>> next = iterator.next();
                if (Objects.nonNull(next.getValue()) && next.getValue().size() > 0) {
                    mxdyMap.put(next.getKey(), next.getValue().get(0));
                }
            }
            jkglRuntimeData.getMxdyList().putAll(mxdyMap);
        }

        jkglRuntimeData.setMxstgxList(new ConcurrentHashMap<>());
        if (Objects.nonNull(sjmxXtMxstgxes) && !sjmxXtMxstgxes.isEmpty()) {
            Map<String, List<TSjmxXtMxstgx>> collect = sjmxXtMxstgxes.stream().collect(Collectors.groupingBy(TSjmxXtMxstgx::getMXBM));
            jkglRuntimeData.getMxstgxList().putAll(collect);
        }


        List<Mxgxdy> mxgxdies = this.exportMapper.listMxgxByXmbh(xmbm);
        if (Objects.nonNull(mxgxdies) && !mxgxdies.isEmpty()) {
            jkglRuntimeData.setMxgxList(mxgxdies.stream().collect(Collectors.groupingBy(Mxgxdy::getMxbm)));
        }

        List<Zddy> zddies = this.exportMapper.listZddyByXmbh(xmbm);
        if (Objects.nonNull(mxgxdies) && !mxgxdies.isEmpty()) {
            jkglRuntimeData.setZddyList(zddies.stream().collect(Collectors.groupingBy(Zddy::getStmc)));
        }
        List<Jkdy> jkdies = this.exportMapper.listJkdyByXmbh(xmbm);
        Map<String, Jkdy> map = new HashMap<>();
        for (Jkdy jkdy : jkdies) {
            map.put(jkdy.getJkbm(), jkdy);
        }
        jkglRuntimeData.setJkdyList(map);

        Map<String, String> jkdzMap = new HashMap<>();
        List<Map<String, String>> jkdzMaps = this.exportMapper.getJkdzMap(xmbm);
        for (Map<String, String> stringStringMap : jkdzMaps) {
            jkdzMap.put(stringStringMap.get("jkdz"), stringStringMap.get("jkbm"));
        }
        jkglRuntimeData.setJkdzMap(jkdzMap);
        return jkglRuntimeData;
    }
}
